*** reset libnames ;
/*%include "&studyroot.\Work\Programs\utility\template\libnames_set.sas";*/
%include "\\algopharm.com\algorithmepharmadata\Departments\Biostatistics\Standard Folder Structure\Macro Repository\test\adam_param_001.sas";
options validvarname=upcase;
%let domain=pc;
%load_tdd_or_not;

/*************************************************************************************************/
*** get source data ;

%get_data(pre=sdtm,lib=sdtm,supp=y,lst=dm se tv ta &domain);
%get_data(pre=adam,lib=adam,lst=adsl);

/*%get_data(pre=sdtm,lib=sdtm,supp=y,lst=dm se tv ta &domain,subm=1);*/
/*%get_data(pre=adam,lib=adam,lst=adsl,subm=1);*/

*** gather info ;
** make param stuff ;
*** create avisit/n, atpt/n, aperiod/c, and aphase/n ;
%adam_param(dom=&domain); 
* default input dataset => pre=sdtm in=&domain from %get_data ;

*** code block
===> add derived stuff that does not require avisit/aperiod/time point, treatment, analysis date/time, values, baseline
;
data apm_fin;
  set apm_fin;
run;

%let no_window=1;
%macro test_if;
%if &no_window %then %do;
   %adam_no_window_awhilo(fu_vnum=&veos);
%end;
%else %do;
   %adam_no_window_awhilo(fu_vnum=&veos);
%end;
%mend test_if;
%test_if;

%adam_timing(dom=&domain,aphase=0); * default input dataset => in=amp_fin from %adam_mk_param and hilo=anwa_fin from %adam_no_window_awhilo ;

*** code block
===> add derived stuff that does not require treatment, analysis date/time, values, baseline
;

*** get the treatments for trtp/a ;
%adam_trt(dom=&domain,byper=1,debug=0); * default input dataset => in=atm_fin from %adam_timing ;

data atr_fin;
set atr_fin;
if trtp=trta and trtpn ne trtan then do; trta=trtp; trtan=trtpn; end;
run;

*** code block
===> add derived stuff that does not require analysis date/time, values, baseline
;

*** make the analysis date/time ;
%adam_adt(dom=&domain,target=1); * default input dataset => in=atr_fin from %adam_trt ;

*** code block
===> add derived stuff that does not require values, baseline
;

*** make the analysis values ;
%adam_aval(dom=&domain); * default input dataset => in=adt_fin from %adam_adt ;

*** code block
===> add derived stuff that does not require baseline
;

*** create analysis records flag ;
%adam_anl01fl(in=avl_fin,by=usubjid param avisitn atptn cond awtdiff ady pcseq,first=cond); * default input dataset => in=abv_fin from %adam_bsl_val ;

*** get decimal precision for each param ;
%get_deci(dom=&domain,lib=sdtm,in=&domain);
proc sql;
create table toder as select a.*,ifn(missing(b.deci),0,b.deci) as nbprec
   from a01_fin as a left join gd_deci as b on a.&domain.testcd=b.&domain.testcd;
quit;

*** derive adam stuff ;
data todef;
   set toder; 

   *** derive other stuff here as necessary ;
run;

*** define listing variables ;
data tolbl;
   set todef;

   *** listing definitions ;
run;

*** any variable not normally in the adam ig should be given a label here ;
proc sort data=tolbl  out=toflg;
by usubjid;
run;

*** table flags ;
%let trt=trtp;
/* flabel=1234567890123456789012345678901234567890*/
 ** flags ;
/* ===> uncomment to create flags if necessary for tables
%mk_flag(in=toflg,flag=aocc01fl,
   whr=dummy_condition,by=&trt usubjid adt lbseq,first=usubjid,
   flabel=dummy_label);
*/

data final_adpc; 
  merge toflg(in=ppp) adam.adsl(keep=usubjid ARM ARMCD ACTARM ACTARMCD); 
by usubjid;
if ppp;
  *if trtp = " " and _trta ne " " then trtp=strip(_trta);

  drop trt01p trt02p trt01pn trt02pn trt02a trt01an trt02an age 
       ethnic race rfendtc rfstdtc sex tr01edt tr01edtm tr01sdt 
       tr01sdtm tr02edt tr02sdt tr02sdtm TR02EDTM trt01a trtedt 
       trtedtm trtsdt trtsdtm;

run;

**Create missing variables;
proc sort data=final_adpc;
  by studyid usubjid param paramcd avisit atpt use;
run;

data final_adpc;
  set final_adpc;
  by studyid usubjid param paramcd avisit atpt use;
  retain aseq 0;

  if first.usubjid then aseq = 1;
  else aseq + 1;

  label
  nbprec   = "Decimal Precision"
  PCSTRESC = "Character Result/Finding in Std Format"
  pcseq    = "Sequence Number"
  atpt     = "Analysis Timepoint"
  atptn    = "Analysis Timepoint (N)"
  avisit   = "Analysis Visit"
  avisitn  = "Analysis Visit (N)"
  AWTARGET = "Analysis Window Target"
  use      = "Use session"
  cond     = "Condition"
               ;

  drop AW: tr03: tr04: tr05: tr06: trt03: trt04: trt05: trt06: aperiod aperiodc;

run;

*** make adam ;
%make_adam(in=final_adpc,dom=&domain,key=studyid usubjid paramcd param avisitn atptn use,debug=0);

** Reorder the variables per Justine;
 proc sql; 
  create table adamw.adpc (label="Pharmacokinetic Concentrations") as
  select STUDYID,USUBJID,SUBJID,SITEID, ARM, ARMCD, ACTARM, ACTARMCD, ASEQ,ENRLFL,RANDAFL,RANDBFL,SAFFL,PKFL,PDFL,complfl,
         TRTP,TRTPN,TRTA,TRTAN,ADT,ATM,ADY,AVISIT,AVISITN,
         ATPT,ATPTN,PARAM,PARAMCD,PARAMN,PARCAT1,PARCAT1N,AVAL,AVALC,COND,USE,
         NBPREC,PCDESC,PCDESCBA,ANL01FL,PCNCA,PCNCABA,PKPOP
    from adam.adpc
    order by studyid,usubjid,paramn;
 quit;

*** clear work and other libnames ;
%clrw;
/*%include "&studyroot.\Work\Programs\utility\template\libnames_clr.sas";*/

